package com.hyh.spring.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hyh.spring.common.Result;
import com.hyh.spring.model.Payment;
import com.hyh.spring.myhandler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsPackage: com.hyh.spring.controller
 * @ClassName RateLimitController
 * @Author: hyhWTX
 * @CreateTime: 2023-06-01  21:22
 * @Description: TODO (一句话描述以下该类的功能)
 * @Version: 1.0
 */
@RestController
public class RateLimitController {
    /**
     * 按资源名称限流+后续处理
     * @return
     */
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public Result byResource()
    {
        return new Result(200,"按资源名称限流测试OK",new Payment(2023L,"serial001"));
    }
    public Result handleException(BlockException exception) {
        return new Result(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    }

    /**
     * 按照Url地址限流+后续处理
     * @return
     */
    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public Result byUrl() {
        return new Result(200,"按url限流测试OK",new Payment(2023L,"serial002"));
    }

    /**
     * 客户自定义限流处理逻辑
     * @return
     */
    @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handleException1")
    public Result customerBlockHandler(){
        return new Result(200,"按客户自定义",new Payment(2023L,"serial003"));
    }

}
